package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("SubjectDAO")
@Transactional
public class SubjectDAOImpl extends AbstractJpaDao<Subject> implements SubjectDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Subject.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public SubjectDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Subject findSubjectById(Integer id) throws DataAccessException {

		return findSubjectById(id, -1, -1);
	}

	@Transactional
	public Subject findSubjectById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findSubjectById", startResult, maxRows, id);
			return (Subject) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Subject> findSubjectByName(String name) throws DataAccessException {

		return findSubjectByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Subject> findSubjectByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findSubjectByName", startResult, maxRows, name);
		return new LinkedHashSet<Subject>(query.getResultList());
	}

	@Transactional
	public Set<Subject> findSubjectByNameContaining(String name) throws DataAccessException {

		return findSubjectByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Subject> findSubjectByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSubjectByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Subject>(query.getResultList());
	}

	@Transactional
	public Set<Subject> findSubjectByDescription(String description) throws DataAccessException {

		return findSubjectByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Subject> findSubjectByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSubjectByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Subject>(query.getResultList());
	}

	@Transactional
	public Set<Subject> findSubjectByDescriptionContaining(String description) throws DataAccessException {

		return findSubjectByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Subject> findSubjectByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findSubjectByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Subject>(query.getResultList());
	}

	@Transactional
	public Subject findSubjectByPrimaryKey(Integer id) throws DataAccessException {
		return findSubjectByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Subject findSubjectByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findSubjectByPrimaryKey", startResult, maxRows, id);
			return (Subject) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Subject> findSubjectsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findSubjectsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Subject> findAllSubjects() throws DataAccessException {

		return findAllSubjects(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Subject> findAllSubjects(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllSubjects", startResult, maxRows);
		return new LinkedHashSet<Subject>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Subject> findSubjectByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Subject>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Subject> findSubjectsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Subject.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Subject> subjects = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return subjects;
	}

	@Transactional
	public Subject findSubjectByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findSubjectByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Subject findSubjectByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findSubjectByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Subject) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Subject entity) {
		return true;
	}
}
